<?php
	class TestManager{
		
		  private $_db;
	
		  public function __construct($db){
				$this->setDb($db);
		  }
		  
		  public function setDb(PDO $db){
				$this->_db = $db;
		  }	
		  
		  public function add(Test $test){// permet d'ajouter un test
				$q = $this->_db->prepare('INSERT INTO test SET intitule = :intitule, duree = :duree');
				$q->bindValue(':intitule', $test->intitule());
				$q->bindValue(':duree', $test->duree());
				$q->execute();
		  }		
		    		
		  public function delete(Test $test){// permet de supprimer un test
			  $this->_db->exec('DELETE FROM test WHERE id = '.$test->id());
		  }
		  
		  public function update(Test $test){// permet de modifier un test
				$q = $this->_db->prepare('UPDATE test SET intitule = :intitule, duree = :duree WHERE id = '.$test->id());
				$q->bindValue(':intitule', $test->intitule());
				$q->bindValue(':duree', $test->duree());
				$q->execute();
		  }
		  
		  public function get($id){// retourne l'objet test d'id correspondant
				$id = (int) $id;
				$q = $this->_db->query('SELECT * FROM test WHERE id = '.$id);
				$donnees = $q->fetch();
				return new Test($donnees);
		  }
		  
		  public function getList(){  // retourne la liste des test
				$test = array();
				$q = $this->_db->query('SELECT * FROM test ORDER BY id');
				while ($donnees = $q->fetch()){
					$test[] = new Test($donnees);
				}
				return $test;
		  }	
		  
		  public function getNbTest(){ // retoune le nombre de tests
				$q = $this->_db->query('SELECT COUNT(*) FROM test');
				$donnees = $q->fetch();
				return $donnees[0];				  
		  }
		  
		  public function contientExercice($idTest, $idExo)
		  {// vérifie qu’un test va avec un exercice
			  	$q = $this->_db->query('SELECT COUNT(*) FROM test_exercice WHERE idRefTest = '.$idTest.' AND idRefExercice = '.$idExo);
				$donnees = $q->fetch();
				if($donnees[0] > 0){
					return true;
				} else {
					return false;
				}
		  }
		  
		  public function deleteAllExercices($idTest)
		  { // supprime tous les exercies d’un test
			  	$q = $this->_db->query('DELETE FROM test_exercice WHERE idRefTest = '.$idTest);
				$donnees = $q->fetch();			  
		  }
	}
?>